package com.gxljc.bear.crawler.base;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 封装从hbase读取数据库的dao
 *
 * @author tanghaitao
 * @since 2015-10-2 下午4:31:54
 */
public class HbaseDao extends Configuration implements Serializable {
    private static final Logger LOG = Logger.getLogger(HbaseDao.class);
    private static final HbaseDao dao = new HbaseDao();


    private static void initConfiguration(Configuration conf) {
        conf.addResource(DolphinCrawlerConsts.FILE_HBASE_CONFIG);
        conf.addResource(DolphinCrawlerConsts.FILE_CORE_SITE);
        conf.addResource(DolphinCrawlerConsts.FILE_HDFS_SITE);
        conf.addResource(DolphinCrawlerConsts.FILE_YARN_SITE);
    }

    private HbaseDao() {
        initConfiguration(this);
    }

    public static HbaseDao getInstance() {
        return dao;
    }


    private String getValue(Result result, String family, String qualifier) {
        byte[] value = result.getValue(family.getBytes(), qualifier.getBytes());
        return Bytes.toString(value);
    }

    private List<Get> getGets(List<String> clusterIdList) {
        List<Get> getList = new ArrayList<Get>();
        for (String id : clusterIdList) {
            Get get = new Get(Bytes.toBytes(id));
            getList.add(get);
        }
        return getList;
    }

}
